package com.turing.post.datatranfer.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.CardStock;
import com.turing.post.cardmanagement.util.CardStockDao;
import com.turing.post.cardmanagement.util.IssueCardStockDetail;
import com.turing.post.cardmanagement.util.IssueCardStockDetailDao;
import com.turing.post.cardmanagement.util.MakeCardControl;
import com.turing.post.cardmanagement.util.MakeCardControlDao;
import com.turing.post.common.util.ContrastDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.datatranfer.util.EntityCard;
import com.turing.post.datatranfer.util.EntityCardBusiness;
import com.turing.post.datatranfer.util.EntityCardDao;
import com.turing.post.datatranfer.util.TranferApplicationException;
import com.turing.post.datatranfer.util.TranferConfig;
import com.turing.post.organization.util.IssueBranch;
import com.turing.post.organization.util.IssueBranchDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 实体卡库存业务实现接口
 * 
 * @author ZXL
 * 
 */
@Stateless(mappedName = "datatranfer.impl.EntityCardBusinessImpl")
@Remote
@Local
public class EntityCardBusinessImpl implements EntityCardBusiness {
	
	@EJB(mappedName = "datatranfer.impl.EntityCardDaoImpl")
	private EntityCardDao entityCardDaoImpl;
	@EJB(mappedName = "cardmanagement.impl.CardStockDaoImpl")
	private CardStockDao cardStockDaoImpl;
	@EJB(mappedName = "cardmanagement.impl.MakeCardControlDaoImpl")
	private MakeCardControlDao makeCardControlDaoImpl;
	@EJB(mappedName = "organization.impl.IssueBranchDaoImpl")
	private IssueBranchDao issueBranchDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;
	@EJB(mappedName = "cardmanagement.impl.IssueCardStockDetailDaoImpl")
	private IssueCardStockDetailDao issueCardStockDetailDaoImpl;
	@EJB(mappedName = "common.impl.ContrastDaoImpl")
	private ContrastDao contrastDaoImpl;
	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDao;

	@Override
	public void getEntityCardListInsertCardStock(String number,String issueCompanyNo)
			throws PCPSApplicationException {
		System.out.println("======="+number);
		List<EntityCard> list = entityCardDaoImpl.getEntityCardList(number);
		CardStock cardStock = null;
		for (EntityCard vo : list) {
			cardStock = new CardStock();
			MakeCardControl makeCardControl = makeCardControlDaoImpl
					.getMakeCardControlByBatchNo(issueCompanyNo+vo.getBatchNo());
			// 检查批次号是否存在
			if (makeCardControl == null) {
				throw new TranferApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "制卡批次号(BatchNo):" + vo.getBatchNo()
								+ "在MakeCardControl表中不存在!");
			}
			String issueBranchNo = contrastDaoImpl.getMapIssueBranchByNo(vo.getBranchNo()).trim();
			//检查机构号在对照表中是否存在
			if (PCPSUtil.isEmpty(issueBranchNo)){
				throw new TranferApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "发卡商机构号:" + vo.getBranchNo()
								+ "在MapIssueBranch表中不存在!");
			}
			IssueBranch issueBranch = issueBranchDaoImpl
					.getIssueBranchByIssueBranchNo(issueBranchNo);
			// 检查机构是否存在
			if (issueBranch == null) {
				throw new TranferApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "所属机构(BranchNo):" + issueBranchNo
								+ "在IssueBranch表中不存在!");
			}
			CardStock obj = cardStockDaoImpl.getCardStockByCardseqno(vo.getCardSeqNo());
			if (obj != null && !PCPSUtil.isEmpty(obj.getBatchNo()))
				continue;
			cardStock.setBatchNo(issueCompanyNo+vo.getBatchNo());
			cardStock.setCardSeqNo(vo.getCardSeqNo());
			cardStock.setCardNo(vo.getCardNo());
			cardStock.setCardPwd(vo.getCardPwd());
			cardStock.setTwoTrackContent(vo.getCardNo());
			cardStock.setThirdTrackContent(vo.getCardNo());
			cardStock.setIssueBranchNo(issueBranchNo);
			cardStock.setStatus(CardStock.CARDSTOCK_STATUS_NORMAL);
			cardStockDaoImpl.insertCardStock(cardStock);

		}
	}

	@Override
	public void insertIssueCardStockDetail(String issueBranchNo,
			String batchNo,BigInteger cardFaceId,BigInteger operId) throws PCPSApplicationException {
		List<CardStock> list = cardStockDaoImpl.getCardStockListByBatchNo(issueBranchNo,
				batchNo);
		IssueCardStockDetail issueCardStockDetail = null;
		int listsize = list.size();
		int i = 0;
		// 遍历已制未售卡库存列表
		for (CardStock vo : list) {
			i++;
			if (issueCardStockDetail == null){
				issueCardStockDetail = new IssueCardStockDetail();
				issueCardStockDetail.setIssueBranchNo(vo.getIssueBranchNo());
				issueCardStockDetail.setCardStartSeqNo(vo.getCardSeqNo());
				issueCardStockDetail.setCardEndSeqNo(vo.getCardSeqNo());
				issueCardStockDetail.setBatchNo(vo.getBatchNo());
				issueCardStockDetail.setOrderCount(1);
				if (i == listsize){
					insertIssueCardStockDetail(issueCardStockDetail,cardFaceId,operId,issueBranchNo);
				}
				continue;
			}
			BigInteger cardEndSeqNo = new BigInteger(issueCardStockDetail.getCardEndSeqNo()).add(BigInteger.ONE);
			BigInteger tempCardSeqNo = new BigInteger(vo.getCardSeqNo());
			if (cardEndSeqNo.compareTo(tempCardSeqNo) == 0) {
				issueCardStockDetail.setCardEndSeqNo(vo.getCardSeqNo());
				issueCardStockDetail.setOrderCount(issueCardStockDetail
						.getOrderCount() + 1);
				if (i == listsize){
					insertIssueCardStockDetail(issueCardStockDetail,cardFaceId,operId,issueBranchNo);
				}
				continue;
			}
			
			insertIssueCardStockDetail(issueCardStockDetail,cardFaceId,operId,issueBranchNo);
			issueCardStockDetail = new IssueCardStockDetail();
			issueCardStockDetail.setIssueBranchNo(vo.getIssueBranchNo());
			issueCardStockDetail.setCardStartSeqNo(vo.getCardSeqNo());
			issueCardStockDetail.setCardEndSeqNo(vo.getCardSeqNo());
			issueCardStockDetail.setBatchNo(vo.getBatchNo());
			issueCardStockDetail.setOrderCount(1);
			if (i == listsize){
				insertIssueCardStockDetail(issueCardStockDetail,cardFaceId,operId,issueBranchNo);
				continue;
			}
			
			// 取当前时间
			//insertIssueCardStockDetail(issueCardStockDetail);
			//issueCardStockDetail = null;
		}
	}
	
	public void insertIssueCardStockDetail(IssueCardStockDetail issueCardStockDetail,
			BigInteger cardFaceId,BigInteger operId,String issueBranchNo)
			throws PCPSApplicationException{
		//插入明细
		IssueCardStockDetail icsd = issueCardStockDetailDaoImpl
				.getIssueCardStockDetailByCardSeqNo(issueCardStockDetail.getCardStartSeqNo(),
						issueCardStockDetail.getCardEndSeqNo());
		if (icsd != null && icsd.getIoOrderId() != null)
			return;
		IssueCompany issueCompany = issueCompanyDao.
				getIssueCompanyByIssueCompanyNo(issueBranchNo.substring(0, 6));
		Date curTime = pcpsCommonDaoImpl.getDatabaseSysTime();
		BigInteger newSaleCreditDepositBillId = pcpsCommonDaoImpl
				.getUniqueIntegerKey();
		issueCardStockDetail.setIoOrderId(newSaleCreditDepositBillId);
		issueCardStockDetail.setCardFaceId(cardFaceId);
		issueCardStockDetail.setTranDate(issueCompany.getTranDate());
		issueCardStockDetail.setTrantime(curTime);
		issueCardStockDetail.setTranType(IssueCardStockDetail.ISSUECARDSTOCKDETAIL_TRANTYPE_WAITMAKE);
		issueCardStockDetail.setOperId(operId);
		issueCardStockDetail.setRemark(TranferConfig.DEFAULT_REMARK);
		issueCardStockDetailDaoImpl.insert(issueCardStockDetail);
	}
}
